﻿using AutoMapper;
using MediatR;
using Traceability.App.DTO;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.Aquaculture
{
    public class HealthQueryCommandHandler : IRequestHandler<HealthQueryCommand, APIResult<List<HealthDto>>>
    {
        private readonly IBaseRepository<T_Health> healthRepository;
        private readonly IMapper mapper;

        public HealthQueryCommandHandler(IBaseRepository<T_Health> healthRepository, IMapper mapper)
        {
            this.healthRepository = healthRepository;
            this.mapper = mapper;
        }

        public Task<APIResult<List<HealthDto>>> Handle(HealthQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<HealthDto>> result = new APIResult<List<HealthDto>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            result.Data = mapper.Map<List<HealthDto>>(healthRepository.GetAll().Where(x=>x.Batch==request.Batch).ToList());
            return Task.FromResult(result);
        }
    }
}
